package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.dao.system.ModuleDao;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
    @Autowired
    private DeptService deptService;
  @Autowired
    private UserService userService;
 @Autowired
    private RoleService roleService;
 @Autowired
    private ModuleDao moduleDao;

    @RequestMapping(value = "/list",name = "进入用户管理页面")
    public String findAll(@RequestParam(defaultValue = "1",name = "page")Integer pageNum,@RequestParam(defaultValue = "10")Integer pageSize){
   PageInfo<User> pageInfo= userService.findPage(getCompanyId(),pageNum,pageSize);
   request.setAttribute("page",pageInfo);
   return "system/user/user-list";
  }
    @RequestMapping(value = "/toAdd",name ="进入用户新增页面")
    public String toAdd(){

        //        查询所有本企业的部门数据
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }

    @RequestMapping(value = "/edit",name ="保存用户方法")
    public String edit(User user){

        if (StringUtils.isEmpty(user.getId())){
            //id为空说明是新增
            user.setCompanyId(getCompanyId());
            user.setCompanyName(getCompanyName());
            user.setCreateTime(new Date());
            user.setId(UUID.randomUUID().toString());
            userService.save(user);
        }
        else {
            //id不为空则说明为修改
            user.setUpdateTime(new Date());
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }
    @RequestMapping(value = "/toUpdate",name ="进入用户修改页面")
    public String toUpdate(String id){
        User user=userService.findById(id);
        request.setAttribute("user",user);
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }
    @RequestMapping(value = "/delete",name ="删除用户信息")
    public String delete(String id){
        userService.deleteById(id);
        return "redirect:/system/user/list.do";
    }
    @RequestMapping(value = "/roleList",name ="进入到用户分配角色页面")
    public String roleList(String id){
   //查询当前用户
        User user = userService.findById(id);
        request.setAttribute("user",user);
        //查询当前企业角色数据
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList",roleList);
        List<String> roleIdList=userService.findRoleIdsByUserId(id);
        String userRoleStr = roleIdList.stream().collect(Collectors.joining(","));
        request.setAttribute("userRoleStr",userRoleStr);
        return "system/user/user-role";
    }
    @RequestMapping(value = "/changeRole",name ="保护用户角色中间表数据")
    public String changeRole(String userid,String[] roleIds){
        userService.changeRole(userid,roleIds);
        return "redirect:/system/user/list.do";
    }

}
